/**
 * Created with JetBrains WebStorm.
 * User: liyong.wang
 * Date: 16/12/2
 * Time: 上午10:14
 * Desc: 研发环境配置文件
 */
var os = require('os');
var path = require("path");
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");

var isProduction = checkIp('10.128.31.91');//生产环境IP检测

module.exports = {
  entry: {
    main: './main.jsx',
    vendor:[
      'react',
      'react-dom',
      'react-router',
      'underscore'
    ]
  },
  output: {
    path: path.resolve(__dirname, 'built'),
    filename: '[name].js?[chunkhash]',
    chunkFilename: '[name].js?[chunkhash]',
    publicPath: '/built/'
  },
  resolve: {
    modulesDirectories: [ 'node_modules' ],
    extensions: ['', '.js', '.jsx']
  },
  devtool: isProduction ? false : 'eval',
  module: {
    loaders: [
      {
        test: /\.jsx|js$/,
        loader: 'babel-loader',
        exclude: /(node_modules|bower_components)/,
        query: {
          presets: ['es2015', 'react']
        }
      },
      {
        test: /\.css|scss$/,
        loader: ExtractTextPlugin.extract("style","css!sass")
      },
      {
        test: /\.png|jpg|gif|jpeg$/,
        loader: "url-loader?limit=8194&name=img/[name].[ext]",
        query: 'random=' + new Date().getTime(),
      },
      {
        test: /\.json$/,
        loader: "json"
      }
    ],
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env':{
        'NODE_ENV': JSON.stringify('development'), //production & development,
        'PUBLIC_PATH': JSON.stringify('http://127.0.0.1')
      }
    }),
    new webpack.optimize.CommonsChunkPlugin({
      names: ['main','vendor'],
      children: true,
      async: true,
      minChunks:2
    }),
    // remove all comments and desc and copyright
    (isProduction ?
        new webpack.optimize.UglifyJsPlugin({output: {comments: false},compress: {warnings: false}}) : function(){}),

    new ExtractTextPlugin("app.css?[hash]", {allChunks: true}),
    // 按引用频度来排序 ID，以便达到减少文件大小的效果
    new webpack.optimize.OccurenceOrderPlugin()
  ]
};


/**
 * Created with JetBrains WebStorm.
 * User: liyong.wang
 * Date: 16/12/2
 * Time: 上午11:04
 * Desc: 获取当前服务器IP地址，以便判定当前环境为生产环境还是研发环境 从而实现在开发环境不压缩
 */
function checkIp(ip){
  var IPv4,
      connections = os.networkInterfaces().en0 ? os.networkInterfaces().en0 : os.networkInterfaces()['本地连接'];
  if(!connections){
    connections = os.networkInterfaces().en5;//有线连接
  }
  if(!connections){//linux
    connections = os.networkInterfaces().eth0;
  }
  for(var i=0;i<connections.length;i++){
    if(connections[i].family=='IPv4'){
      IPv4=connections[i].address;
    }
  }
  return IPv4 == ip;
}